package com.demo.example.goods.dao;

import com.demo.example.goods.domain.Goods;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * Created by azhai on 2017/4/20.
 */
public interface GoodsRepository extends JpaRepository<Goods, Long> {

    List<Goods> findAllByOwner(Long owner);

    Goods findOneByGoodsNum(Long num);

//    @Modifying(clearAutomatically = true)
    @Modifying
    @Query("UPDATE goods SET lockUser = ?1 WHERE lockUser is NULL and goodsNum = ?2")
    int lockGoods(Long customerId, Long goodsNum);

    @Modifying
    @Query("UPDATE goods SET lockUser = null WHERE lockUser = ?1 and goodsNum = ?2")
    int unLockGoods(Long customerId, Long goodsNum);

    @Modifying
    @Query("UPDATE goods SET owner = ?1, lockUser = null WHERE lockUser = ?1 and goodsNum = ?2")
    int moveGoods(Long customerId, Long goodsNum);

    @Modifying
    @Query("UPDATE goods SET owner = NULL WHERE owner = ?1 and goodsNum = ?2")
    int unMoveGoods(Long customerId, Long goodsNum);

    @Override
    @Modifying(clearAutomatically = true)
    Goods save(Goods o);

}
